import com.google.gson.Gson
import io.vertx.config.ConfigChange
import io.vertx.config.ConfigRetriever
import io.vertx.config.ConfigRetrieverOptions
import io.vertx.config.ConfigStoreOptions
import io.vertx.core.AsyncResult
import io.vertx.core.Vertx
import io.vertx.core.http.HttpServer
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.Router
import io.vertx.ext.web.handler.StaticHandler
import org.apache.velocity.Template
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.apache.velocity.app.VelocityEngine
import java.io.File
import java.io.StringWriter
import java.util.*


class Test {
    var test = "我是一条测试";
    fun getTestString(): String {
        return test;
    }
}

fun main(args: Array<String>) {
    var vertx = Vertx.vertx()
    val server: HttpServer = vertx.createHttpServer()

    val ve = VelocityEngine()

    ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "www");

    val vprops = Properties()
    vprops.put(Velocity.INPUT_ENCODING, "UTF-8")

    ve.init(vprops)

    val ctx = VelocityContext()
//    ctx.put("name", "velocity")
//    var map = mapOf<String, Any>(
//        "role" to "test"
//    )
//    ctx.put("map", Test())

    //==========================================================
    val store: ConfigStoreOptions = ConfigStoreOptions()
        .setType("file")
        .setFormat("yaml")
        .setConfig(
                JsonObject()
                        .put("path", "config/config.yaml")
        )

    val retriever: ConfigRetriever = ConfigRetriever.create(
            vertx,
            ConfigRetrieverOptions().addStore(store)
    )



    retriever.getConfig { ar: AsyncResult<JsonObject?> ->
        if(ar.failed()){
            print(ar.cause())
        }else{
            val gson = Gson()
            var ret =  gson.fromJson(ar.result().toString(), Resume::class.java)
            ctx.put("resume", ret)
        }
    }

    retriever.listen { change: ConfigChange ->
        // Previous configuration
        val previous = change.previousConfiguration
        // New configuration
        val conf = change.newConfiguration

        val gson = Gson()
        var ret =  gson.fromJson(change.newConfiguration.toString(), Resume::class.java)
        ctx.put("resume", ret)
    }
    //==========================================================

    val router = Router.router(vertx)
    router.route("/*").handler {
        var path = it.normalizedPath()
        var file = File("www$path");
        var fileName = file.getName();
        var fileTyle=fileName.substring(fileName.lastIndexOf("."), fileName.length);
        if (fileTyle == ".html"){
            if (!file.exists()){
                it.end("no found this file")
            }else{
                val sw = StringWriter()
                val t: Template = ve.getTemplate(path, "UTF-8")
                t.merge(ctx, sw)
                it.end(sw.toString())
            }
            return@handler
        }
        it.next()
    }
    router.route("/*").handler(StaticHandler.create("www").setCachingEnabled(false));

    server.requestHandler(router);
    server.listen(8089)
}